import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/schooladmin.dart';
import 'package:ichinesereader/controller/user.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/studentsAddDatagrid.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';

class AddStudentsToClassPage extends StatelessWidget {
  final box = Hive.box('appconfig');

  @override
  Widget build(BuildContext context) {
    Log.debug('build AddStudentsToClassPage');

    final SchoolAdminController _schoolAdminController = Get.find();
    final UserController _userController = Get.find();
    String? classId;

    bool isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Teacher;

    if (isTeacher) {
      classId = _userController.currentClass.value.classId;
      Future.delayed(Duration(milliseconds: 100), () {
        if (classId != '')
          _schoolAdminController
              .fetchAllStudentsNotInGivenClass(classId.toString());
      });
    }

    return Scaffold(
      appBar: CustomAppBar(title: 'Add Students'.tr),
      // drawer: TeacherAppDrawer(),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            _schoolAdminController.obx(
              (data) => Expanded(
                  child: StudentsAddDataGrids(
                      false, _schoolAdminController.studentItemsNotInClass)),
              onEmpty: StudentsAddDataGrids(
                  false, _schoolAdminController.studentItemsNotInClass),
              onLoading: Container(),
              // onLoading: Center(
              //     child: CircularProgressIndicator(
              //   backgroundColor: Get.theme.primaryColorDark,
              //   strokeWidth: 9.0,
              // ))
            ),
          ],
        ),
      ),
    );
  }
}
